﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System;
using System.IO;

namespace Lcsoft.UrlRewriter.Conditions
{
	/// <summary>
	/// 表示请求地址包含的文件是否存在的先决条件。
	/// </summary>
	public class ExistsCondition : IRewriteCondition
	{
		private readonly string _location;

		/// <summary>
		/// 初始化 <see cref="ExistsCondition"/> 类的新实例。
		/// </summary>
		/// <param name="location">请求地址。</param>
		public ExistsCondition(string location)
		{
			if (location == null) throw new ArgumentNullException("location");

			this._location = location;
		}

		#region IRewriteCondition Members

		/// <summary>
		/// 判断条件是否满足。
		/// </summary>
		/// <param name="context"><see cref="RewriteContext"/> 对象的实例。</param>
		/// <returns>如果条件满足则返回 true，否则返回 false。</returns>
		public bool IsMatch(RewriteContext context)
		{
			if (context == null) throw new ArgumentNullException("context");

			var filename = context.MapPath(context.Expand(this._location));

			return File.Exists(filename) || Directory.Exists(filename);
		}

		#endregion
	}
}
